package com.school.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.school.model.Eatery;
import com.school.service.impl.EateryServiceImpl;

public class EateryAction
{
    EateryServiceImpl eateryService = new EateryServiceImpl();

    // �͹ݵ�¼
    public String eLogin()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String eLoginId = request.getParameter("eLoginId");
        String eateryPass = request.getParameter("eateryPass");
        Eatery eatery = eateryService.eateryLogin(eLoginId, eateryPass);
        if (eatery != null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("Eatery", eatery);
            return "index";
        }
        return "error";
    }

    // �޸Ĳ͹���Ϣ
    public String alterEatery()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String eateryName = request.getParameter("eateryName");
        String legalMan = request.getParameter("legalMan");
        String did = request.getParameter("districtId");
        int districtId = Integer.parseInt(did);
        String businessTime = request.getParameter("businessTime");
        String area = request.getParameter("area");
        String eAddress = request.getParameter("eAddress");
        String ephone = request.getParameter("ephone");
        String eEmail = request.getParameter("eEmail");
        String synopsis = request.getParameter("synopsis");
        String eid = request.getParameter("eateryId");
        int eateryId = Integer.parseInt(eid);

        int result = eateryService.alterEateryDetail(eateryName, legalMan,
                districtId, businessTime, area, eAddress, ephone, eEmail,
                synopsis, eateryId);
        if (result < 0)
        {
            return "error";
        }
        else
        {
            return "success";
        }
    }

    // �޸Ĳ͹�����
    public String alterEateryPwd()
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String id = request.getParameter("eateryId");
        int eateryId = Integer.parseInt(id);

        String eateryPass = request.getParameter("eateryPass");
        String eateryPass1 = request.getParameter("eateryPass1");

        // �ж��������������Ƿ�һ��
        if (eateryPass.equals(eateryPass1))
        {
            int result = eateryService.alterEateryPwd(eateryPass1, eateryId);
            if (result < 0)
            {
                return "error";
            }
            else
            {
                return "success";
            }
        }
        else
        {
            request.setAttribute("msg2", "�����������벻һ�£����������룡");
            return "alterEateryPass";
        }

    }

}
